<template>
    <div class="approval">
        <div class="approval_header">
            <div class="approval_header_top">
                <div class="title">
                    我的审批
                </div>
                <div class="approval_header_top_right">
                    <div class="date">
                        <div class="date_div" v-for="(item, index) in dateList" :key="index"
                            @click="dateClick(index, item.key)" :class="dateIndex == index && 'date_div_active'">
                            {{ item.tilte }}
                        </div>
                    </div>
                    <van-image :src="menuIcon" class="menu_cion" @click="showRight = true" />
                </div>
            </div>
            <div class="approval_header_bottom">
                <div class="approval_header_bottom_div" v-for="(item, index) in dataNums" :key="index">
                    <div class="approval_line" v-show="index"></div>
                    <div class="approval_nums">
                        {{ item.nums }}
                    </div>
                    <div class="approval_titles">
                        {{ item.title }}
                    </div>
                </div>
            </div>
            <div class="home-index-notice">
                <van-image class="notice_icon" :src="gonggao" />
                <van-notice-bar class="font" scrollable :text="versionNotice" />
            </div>
        </div>
        <SystemSettings v-model="showRight"></SystemSettings>
        <div class="todayAddition">
            <van-image :src="dropDown"></van-image>
            {{ title }}
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div"
                        @click="getDetails(item.id, auditStatusNameById(item.status))">
                        <van-image :src="item?.productImages" class="van-image">
                            <template v-slot:error>加载失败</template>
                            <div class="liveStreamingMechanisms" v-if="item.hmMechanismsList[1]?.cooperationMode">多机制
                            </div>
                        </van-image>
                        <div class="div_right">
                            <div class="div_right_top">
                                <div class="style_name">
                                    <van-text-ellipsis :content="item.styleName" />
                                </div>
                                <div class="auditStatus" :style="auditStatusStyle(item.status)">
                                    {{ auditStatusNameById(item.status) }}
                                </div>
                            </div>
                            <div class="div_right_center">
                                <van-text-ellipsis rows="1" :content="item.sellingPoints" />
                            </div>
                            <div class="div_right_bottom">
                                <div class="price" v-if="item.hmMechanismsList[0]?.cooperationMode == '分佣'">￥ {{
                                    item.hmMechanismsList[0].price }}</div>
                                <div class="price" v-if="item.hmMechanismsList[0]?.cooperationMode == '供货'">￥ {{
                                    item.hmMechanismsList[0].costPrice }}</div>
                                <div class="costPrice" v-if="item.hmMechanismsList[0]?.cooperationMode == '分佣'">{{
                                    item.hmMechanismsList[0].costPrice }}</div>
                                <div class="commission" v-if='item.hmMechanismsList[0]?.commission'>
                                    佣 {{ (item.hmMechanismsList[0].commission || 0) + "%" }}
                                </div>
                            </div>
                            <div class="div_right_people">
                                <div class="launch">发起人： {{ item.recruiter }}</div>
                                <div class="approvals">审批人：{{ item.auditor }}</div>
                            </div>
                        </div>
                        <div class="div_right_times">
                            <div class="dates">发起时间：{{ item.createTime }}</div>
                            <div class="liveStreamingMechanism">{{ item.hmMechanismsList[0]?.liveStreamingMechanism }}
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>

        <van-tabbar v-model="activeName" fixed>
            <van-tabbar-item v-for="tabbarItem in tabbarList" :key="tabbarItem.id" :to="tabbarItem.to"
                :name="tabbarItem.name">
                <template #icon="">
                    <svg-icon :icon="tabbarItem.icon" />
                </template>
                {{ tabbarItem.title }}
            </van-tabbar-item>
        </van-tabbar>
    </div>
</template>
<script setup lang="ts">
import SystemSettings from '@/components/SystemSettings/index.vue'
import { auditStatusNameById } from "@/utils/statusNameById";
import { onMounted, ref } from 'vue';
import { getEnvValue } from "@/utils/index";
import { useRouter } from "vue-router";
import { listNotice } from '@/api/loginApi'
import { stripTags } from '@/utils/index'
import { getQualityList, getSystemList, getQualityStatus } from '@/api/approval/index'
const dropDown = new URL("@/assets/approval/icon_jinrixinzeng.png", import.meta.url).href
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
const gonggao = new URL("@/assets/home/icon_gonggao.png", import.meta.url).href;
const router = useRouter();
const versionNotice = ref()

const dateList = ref([
    { tilte: '日', key: '1' },
    { tilte: '周', key: '7' },
    { tilte: '月', key: '30' }
])
const dateIndex = ref(0)
const dataNums = ref([
    { title: '待处理', nums: '0' },
    { title: '已通过', nums: '0' },
    { title: '已驳回', nums: '0' },
])
interface TabItem {
    id: number,
    name: string,
    icon: string,
    title: string,
    to: string
}
const activeName = ref("qualityControl");
const tabbarList: TabItem[] = [
    { id: 1, name: 'qualityControl', icon: 'icon_shouye', title: '首页', to: '/qualityControl' },
    { id: 2, name: 'needdealt', icon: 'icon_daibanweixuanzhong', title: '待办', to: '/pendingQualityControl/待处理' },
    { id: 3, name: 'pass', icon: 'icon_yitongguoweixuanzhong', title: '已通过', to: '/pendingQualityControl/已通过' },
    { id: 4, name: 'rejected', icon: 'icon_yibohuiweixuanzhong', title: '已驳回', to: '/pendingQualityControl/已驳回' },
]
const title = ref('今日新增')
const showRight = ref(false)
const menuIcon = new URL("@/assets/approval/icon_baisecaidan.png", import.meta.url).href;

const categoryList = ref([])
const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    // auditDept == 1 为品控部门列表
    auditDept: 1,
    startTime: '1'
});
// 限制连续刷新数据重复问题
const refreFlag = ref(true)
/** 获取招商品类列表 */
const getsList = async () => {
    const res = await getSystemList({ parentId: '1796913597400784898' })
    if (res.data.length) {
        res.data.unshift({
            deptName: '全部',
            deptId: null
        })
        res.data.forEach(item => {
            item.name = item.deptName
            item.status = item.deptId
        })
        categoryList.value = res.data
    }
}
getsList()
/** 获取三种状态数量 */
const handleStatus = async (startTime = '1') => {
    const res = await getQualityStatus({ startTime: startTime })
    dataNums.value[0].nums = res.rows[0].productsWaitAudit
    dataNums.value[1].nums = res.rows[0].productsPassed
    dataNums.value[2].nums = res.rows[0].productsRejected
}
handleStatus()
/** 状态颜色 */
const auditStatusStyle = (auditStatus: string) => {
    // 待处理
    if (auditStatus == '0') {
        return {
            'background': '#EBF4FF',
            'color': '#0094FF'
        }
        // 已通过
    } else if (auditStatus == '2') {
        return {
            'background': '#E7FDEE',
            'color': '#19D11B'
        }
        //已驳回
    } else if (auditStatus == '1') {
        return {
            'background': '#FDE7E7',
            'color': '#FC2540'
        }
    }
}
/** 日期筛选 */
const dateClick = (index: number, key: string) => {
    dateIndex.value = index;
    queryParams.value.startTime = key;
    if (index == 0) {
        title.value = '今日新增'
    } else if (index == 1) {
        title.value = '本周新增'
    } else if (index == 2) {
        title.value = '本月新增'
    }
    // handleStatus(key)
    onRefresh()
}

/** 获取详情 */
const getDetails = (id: string, name?: string) => {
    router.push({ path: `/pendingQualityControlDetail/${id}`, query: { name: name } })
};

/** 查询商品列表 */
const getList = async () => {
    const res = await getQualityList(queryParams.value);
    res.rows.forEach(async (item) => {
        if (item.productImage) {
            item.productImages = getEnvValue("VITE_APP_PIC_URL") + item.productImage.split(",")[0]
        } else {
            item.productImages = "";
        }
    });
    setTimeout(() => {
        refreFlag.value = true
        // 空数据 停止加载
        if (res.rows.length == 0) {
            list.value = [];
            finished.value = true;
            return;
        }
        // 有数据 赋值列表 加载状态结束
        res.rows.forEach((item) => {
            finished.value = false;
            list.value.push(item);
        });
        loading.value = false;
        refreshing.value = false;
        // 如果list长度大于等于总数据条数，数据全部加载完成
        if (list.value.length >= res.total) {
            finished.value = true; //结束加载
        }
    }, 400);
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value) {
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
        handleStatus(queryParams.value.startTime)
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};

/** 获取版本更新公告 */
const getNotice = async () => {
    const res = await listNotice({})
    const privacyNotice = res.rows.find(item => item.noticeTitle === '版本更新公告');
    if (privacyNotice) {
        versionNotice.value = stripTags(privacyNotice.noticeContent);
    }
}
getNotice()
onMounted(() => { });
</script>
<style lang="less" scoped>
.approval {
    background-color: #F6FAFF;
    width: 100%;
    height: calc(100vh - 44px);
    border-radius: 5px;
    box-sizing: border-box;

    :deep(.van-image__img) {
        border-radius: 5px !important;
    }

    .liveStreamingMechanisms {
        position: absolute;
        top: 0;
        left: 0;
        width: 50px;
        height: 20px;
        background: #0094FF;
        border-radius: 2px 0px 2px 0px;
        font-weight: 400;
        font-size: 12px;
        color: #FFFFFF;
        text-align: center;
        line-height: 20px;
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }

    }

    .approval_header {
        width: 100%;
        height: 200px;
        background: linear-gradient(#0064FF 0%, #008FFF 100%);
        padding-top: 44px;
        box-sizing: border-box;

        .home-index-notice {
            margin-left: 15px;
            margin-right: 15px;
            width: calc(100% - 30px);
            box-sizing: border-box;
            height: 35px;
            background: linear-gradient(180deg, #DDF0FE 0%, #FFFFFF 100%);
            border-radius: 5px;
            margin-top: 15px;
            display: flex;
            align-items: center;
            padding: 0 15px;

            .notice_icon {
                width: 14px;
                height: 14px;
            }

            .font {
                font-weight: 400;
                font-size: 14px;
                color: #1A1B1C;
                margin-left: 10px;
                width: calc(100% - 30px);
                height: 35px;
            }

            :deep(.van-notice-bar) {
                background: none;
                padding: 0;
            }
        }

        .approval_header_top {
            display: flex;
            padding: 0 15px;
            box-sizing: border-box;
            justify-content: space-between;

            .title {
                font-size: 18px;
                color: #FFFFFF;
            }

            .approval_header_top_right {
                display: flex;
                align-items: center;
            }

            .date {
                width: 106px;
                height: 24px;
                background: #D4EDFF;
                border-radius: 12px;
                display: flex;
                font-size: 14px;
                color: #1A1B1C;
                justify-content: space-around;
                align-items: center;

                .date_div {
                    width: 30%;
                    line-height: 24px;
                    text-align: center;
                }

                .date_div_active {
                    background: #0094FF;
                    border-radius: 12px;
                    color: #FFFFFF;
                    line-height: 20px;
                    height: 20px;
                    width: 30%;
                }
            }
        }

        .approval_header_bottom {
            width: 100%;
            color: #FFFFFF;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-sizing: border-box;
            padding: 0 42px;
            align-items: center;
            margin-top: 20px;

            .approval_header_bottom_div {
                width: 33%;
                text-align: center;
                position: relative;

                .approval_line {
                    position: absolute;
                    width: 1px;
                    height: 15px;
                    border-left: 1px solid #F8F8F8;
                    opacity: 0.5;
                    top: 20px;
                }

                .approval_nums {
                    font-weight: 500;
                    font-size: 21px;
                    color: #FFFFFF;
                }

                .approval_titles {
                    font-weight: 400;
                    font-size: 14px;
                    color: #FFFFFF;
                    margin-top: 10px;
                }
            }
        }
    }

    .todayAddition {
        height: 40px;
        width: 100%;
        display: flex;
        background: linear-gradient(270deg, #E1E3FB 0%, #E2F2FF 100%);
        font-weight: 400;
        font-size: 14px;
        color: #0094FF;
        align-items: center;
        padding: 0 10px;
        box-sizing: border-box;

        .van-image {
            width: 20px;
            height: 20px;
        }
    }

    .menu_cion {
        width: 14px;
        height: 14px;
        margin-left: 10px;
        // position: absolute;
        // right: 15px;
    }

    .right_popup {
        box-sizing: border-box;
        padding: 50px 15px;
        color: #1C1D1E;
        display: flex;
        flex-wrap: wrap;
        height: 100%;
        align-content: space-between;

        .right_popup_box {
            width: 100%;

            .right_popup_box_item {
                width: 100%;
                padding: 10px 0;
                border-bottom: 1px solid #efefef;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .right_popup_box_item_left {
                    display: flex;
                    align-items: center;

                    img {
                        width: 14px;
                        height: 14px;
                        margin-right: 20px;
                    }
                }
            }
        }

        .supplier_detail_content_divorder {
            border-bottom: 1px solid #fafaff;
        }

        .supplier_detail_content_div_first {
            margin-top: 63vh;
        }

        .supplier_detail_content_div {
            font-size: 13px;
            height: 35px;
            // height: 25%;
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;

            :deep(.van-icon) {
                margin-left: 0% !important;
            }

            .label {
                min-width: 55px;
            }

            .van-image {
                width: 32px;
                height: 32px;
            }

            .value {
                color: #b8beca;
                max-width: calc(100% - 85px);
                display: flex;
                justify-content: right;
                word-break: break-all;
            }
        }

        .login-index-policy-text-rules {
            font-size: 13px;
            width: 100%;
            margin-top: 65vh;
        }

        .login-index-policy-text-rule {
            font-size: 13px;
            width: 100%;
        }

        .quit_button {
            width: 100%;
            height: 44px;
            background: #FDE7E7;
            border-radius: 5px;
            text-align: center;
            color: #FC2540;
            line-height: 44px;

            .quit_cion {
                width: 14px;
                height: 14px;
            }
        }
    }

    .approval_title {
        display: flex;
        width: 100%;
        height: 90px;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
        padding: 10px 15px;
        font-size: 14px;

        .approval_title_div {
            width: 105px;
            height: 64px;
            position: relative;

            .approval_title_div_name {
                position: absolute;
                top: 24px;
                right: 14px;
            }
        }
    }

    .approval_screen {
        height: 44px;
        width: 100%;
        background-color: #fff;
        display: flex;
        justify-content: space-around;
        align-items: center;
        font-size: 14px;

        :deep(.van-image) {
            width: 10px;
            height: 6px;
            margin-left: 4px;
        }

        .approval_screen_category {
            display: flex;
            align-items: center;
        }

        .approval_screen_date {
            display: flex;
            align-items: center;
        }
    }

    :deep(.van-pull-refresh) {
        height: calc(100vh - 290px);
        overflow: scroll;
    }

    .product_list_van_list {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        margin-top: 10px;
        background: #f6faff;

        .van-image {
            width: 80px;
            height: 80px;
        }

        &_div {
            width: 100%;
            height: 130px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 5px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            padding: 10px 15px 10px 15px;
            display: flex;
            align-items: center;
        }

        .div_right {
            width: calc(100% - 90px);
            display: flex;
            flex-wrap: wrap;

            .div_right_top {
                margin-top: -5px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;
                // padding-left: 10px;

                .style_name {
                    font-size: 14px;
                    font-weight: bold;
                    width: 150px;
                    display: -webkit-box;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    -webkit-line-clamp: 1;
                    -webkit-box-orient: vertical;
                }

                .auditStatus {
                    font-size: 14px;
                    border-radius: 2px;
                    padding: 3px 13px 3px 13px;
                    text-align: center;
                    border-radius: 3px;
                }

                .stats {
                    background: #fde7e7;
                    border-radius: 2px;
                    padding: 3px 13px 3px 13px;
                    font-size: 14px;
                    color: #fc2540;
                    text-align: center;
                    border-radius: 3px;
                }
            }

            .div_right_center {
                // padding-top: 5px;
                margin-top: 5px;
                // padding-left: 10px;
                width: 100%;
                // height: 28px;
                font-size: 12px;
                color: #b8beca;
                display: -webkit-box;
                overflow: hidden;
                text-overflow: ellipsis;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }

            .div_right_bottom {
                margin-top: 2px;
                margin-bottom: 3px;
                // padding-left: 10px;
                display: flex;
                width: 100%;
                align-items: center;
                justify-content: space-between;

                .price {
                    font-size: 12px;
                    color: #fc2540;
                    font-weight: 500;
                    white-space: nowrap;
                }

                .costPrice {
                    font-size: 11px;
                    color: #a3a3a3;
                    font-weight: 500;
                    white-space: nowrap;
                    margin-left: 10px;
                }

                .commission {
                    font-weight: 500;
                    font-size: 12px;
                    color: #fc2540;
                    // border: solid 1px black;
                    white-space: nowrap;
                    width: 20%;
                    margin-left: 20px;
                }

                .liveStreamingMechanism {
                    font-size: 11px;
                    color: #a3a3a3;
                    margin-left: 15px;
                    //   width: 20%;
                    // text-align: right;
                }

                .anchor {
                    margin-left: auto;
                    padding-left: 15px;
                    font-weight: 400;
                    font-size: 12px;
                    color: #b8beca;
                    //   width: 20%;
                    //   border: 1px solid #ffb0b0;
                    //   padding: 1px 2px;
                    text-align: center;
                    white-space: nowrap;
                    /* 确保文本在一行内显示 */
                    overflow: hidden;
                    /* 隐藏溢出的内容 */
                    text-overflow: ellipsis;
                }
            }

            .div_right_people {
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 12px;
                // padding-left: 10px;
                width: 100%;
                flex-wrap: wrap;

                .approvals {
                    // margin-right: 25px;
                    // min-width: 100px;
                }
            }
        }

        .div_right_times {
            width: 100%;
            height: 20px;
            font-size: 12px;
            color: #B8BECA;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }

    :deep(.van-action-sheet__header) {
        font-weight: 500 !important;
        font-size: 16px !important;
        color: #1a1b1c !important;
    }

    :deep(.van-action-sheet__item) {
        font-size: 14px;
        color: #b8beca;
    }

    :deep(.van-tabbar) {
        height: 50px !important;
    }
}
</style>